﻿using UnityEngine;
using System.Collections;

public class ChatBubble : MonoBehaviour {

    [HideInInspector]
    public GameObject m_pOwner = null;

    bool m_bFadeOut;
    public string m_strText;

	// Use this for initialization
    void Start()
    {
        transform.localScale = new Vector3(0.25f, 0.25f);

        transform.GetChild(0).GetChild(1).GetComponent<UILabel>().text = m_strText;
        transform.GetChild(0).GetChild(0).GetComponent<UISprite>().SetDimensions(100, 125 * (transform.GetChild(0).GetChild(1).GetComponent<UILabel>().height / 250));
        transform.GetChild(0).localPosition = new Vector3(0, (transform.GetChild(0).GetChild(1).GetComponent<UILabel>().height / 250) * 90); //240

        GetComponent<UIFollowTarget>().target = m_pOwner.transform;
        GetComponent<UIFollowTarget>().gameCamera = GameObject.Find("MainCamera").GetComponent<Camera>();
        GetComponent<UIFollowTarget>().uiCamera = GameObject.Find("Camera").GetComponent<Camera>();
        GetComponent<UIFollowTarget>().enabled = true;

        StartCoroutine(DestroyTimer());
	}

    void Update()
    {
        if (m_bFadeOut)
        {
            transform.GetChild(0).GetComponent<UISprite>().color = new Color(1, 1, 1, transform.GetChild(0).GetComponent<UISprite>().color.a - 2 / 255f);

            if (transform.GetChild(0).GetComponent<UISprite>().color.a <= 0)
            {
                Destroy(gameObject);
                m_pOwner.GetComponent<UnitStatus>().m_unitData.bTalking = false;
            }
        }
    }

    IEnumerator DestroyTimer()
    {
        yield return new WaitForSeconds(transform.GetChild(0).GetChild(1).GetComponent<UILabel>().height / 250 * 1.0f);
        m_bFadeOut = true;
    }

  
}
